/*
 * Copyright 2013-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.tuling.tlmallorder.service.impl;


import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.tuling.tlmallcommon.BusinessException;
import org.tuling.tlmallcommon.Result;
import org.tuling.tlmallorder.entity.Order;
import org.tuling.tlmallorder.feign.AccountServiceFeignClient;
import org.tuling.tlmallorder.feign.StorageServiceFeignClient;
import org.tuling.tlmallorder.feign.dto.AccountDTO;
import org.tuling.tlmallorder.feign.dto.StorageDTO;
import org.tuling.tlmallorder.mapper.OrderMapper;
import org.tuling.tlmallorder.service.OrderService;

import java.sql.Timestamp;
import java.util.List;

import static org.tuling.tlmallcommon.ResultEnum.COMMON_FAILED;


@Service
public class OrderServiceImpl implements OrderService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountServiceFeignClient accountService;

    @Autowired
    private StorageServiceFeignClient storageService;

    @Autowired
    RestTemplate restTemplate;

    @Override
    @GlobalTransactional(name="createOrder",rollbackFor=Exception.class)
    public Result<?> createOrder(String userId, String commodityCode, Integer count) {

        logger.info("[createOrder] current XID: {}", RootContext.getXID());

        // deduct storage
        StorageDTO storageDTO = new StorageDTO();
        storageDTO.setCommodityCode(commodityCode);
        storageDTO.setCount(count);
        //RestTemplate远程调用
        //String storage_url = "http://localhost:8010/storage/reduce-stock";
        //整合了Nacos+LoadBalaner,可以使用微服务名tlmall-storage代替localhost:8020
        //String storage_url = "http://tlmall-storage/storage/reduce-stock";
        //Integer storageCode = restTemplate.postForObject(storage_url,storageDTO, Result.class).getCode();
        //openFeign远程调用
        Integer storageCode = storageService.reduceStock(storageDTO).getCode();
        if (storageCode.equals(COMMON_FAILED.getCode())) {
            throw new BusinessException("stock not enough");
        }

        // deduct balance
        int price = count * 2;
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setUserId(userId);
        accountDTO.setPrice(price);
        //RestTemplate远程调用
        //String account_url = "http://localhost:8020/account/reduce-balance";
        //整合了Nacos+LoadBalaner,可以使用微服务名tlmall-account代替localhost:8020
        //String account_url = "http://tlmall-account/account/reduce-balance";
        //Integer accountCode = restTemplate.postForObject(account_url, accountDTO, Result.class).getCode();
        //openFeign远程调用
        Integer accountCode = accountService.reduceBalance(accountDTO).getCode();
        if (accountCode.equals(COMMON_FAILED.getCode())) {
            throw new BusinessException("balance not enough");
        }

        // save order
        Order order = new Order();
        order.setUserId(userId);
        order.setCommodityCode(commodityCode);
        order.setCount(count);
        order.setMoney(price);
        order.setCreateTime(new Timestamp(System.currentTimeMillis()));
        order.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        orderMapper.saveOrder(order);
        logger.info("[createOrder] orderId: {}", order.getId());

        return Result.success(order);
    }

    @Override
    public Result<?> getOrderByUserId(String userId) {
        List<Order> list = orderMapper.getOrderByUserId(userId);

        return Result.success(list);
    }

}
